package com.cn.qgx428.orderingmeal;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.dialog.MineBankCard;
import com.cn.qgx428.orderingmeal.dialog.MineInfoDialog;
import com.cn.qgx428.orderingmeal.myClass.Bankcard;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.User;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.helper.BmobNative;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class Mine extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "Mine";

    private MyKeyInfo keyInfo;

    private TextView backbtn;

    private LinearLayout mineInfo;
    private LinearLayout mineBankCard;
    private LinearLayout mine_car;
    private LinearLayout mine_order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);
        Bmob.initialize(this,"1b925fe2ccbb402f4034a3e28c31ad93");
        init();
    }

    private void init() {
        keyInfo = (MyKeyInfo) getIntent().getSerializableExtra("keyInfo");

        backbtn = (TextView)findViewById(R.id.mine_back);
        backbtn.setOnClickListener(this);
        mineInfo = (LinearLayout)findViewById(R.id.mine_info);
        mineInfo.setOnClickListener(this);
        mineBankCard = (LinearLayout)findViewById(R.id.mine_bankCard);
        mineBankCard.setOnClickListener(this);
        mine_car = (LinearLayout)findViewById(R.id.mine_car);
        mine_car.setOnClickListener(this);
        mine_order = (LinearLayout) findViewById(R.id.mine_order);
        mine_order.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_info:
                showMineInfoDialog();
                break;
            case R.id.mine_bankCard:
                showMindBankCard();
                break;
            case R.id.mine_car:
                Intent intent = new Intent(Mine.this,CarActivity.class);
                intent.putExtra("keyInfo",keyInfo);
                startActivity(intent);
                break;
            case R.id.mine_back:
                finish();
                break;
            case R.id.mine_order:
                Intent iten = new Intent(Mine.this,OrderActicity.class);
                iten.putExtra("keyInfo",keyInfo);
                startActivity(iten);
        }
    }

    private void showMindBankCard() {
        final MineBankCard dialog;
        dialog = new MineBankCard(Mine.this, keyInfo.getUserID());
        dialog.show();
        //添加银行卡操作
        dialog.setSubmitBtn(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isADDBankcard()){
                    String CardID = dialog.getCardID();
                    if (CardID.length() < 19){
                        Toast.makeText(Mine.this, "银行卡号长度不正确", Toast.LENGTH_SHORT).show();
                    } else if ("无此类型银行卡".equals(dialog.getCardCat())){
                        Toast.makeText(Mine.this, "卡号信息有误", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        final Bankcard bankcard = new Bankcard();
                        bankcard.setCardID(CardID);
                        BmobQuery<Bankcard> query = new BmobQuery<Bankcard>();
                        query.addWhereEqualTo("cardID",CardID);
                        Log.d(TAG, "onClick: 获取银行卡");
                        query.findObjects(new FindListener<Bankcard>() {
                            @Override
                            public void done(List<Bankcard> list, BmobException e) {
                                if ( e== null){
                                    if (list.size()>0){
                                        Toast.makeText(Mine.this, "卡号已存在", Toast.LENGTH_SHORT).show();
                                    } else {
                                        User user = new User();
                                        user.setObjectId(keyInfo.getUserID());
                                        bankcard.setUser(user);
                                        bankcard.setCardName(dialog.getCardCat());
                                        bankcard.save(new SaveListener<String>() {
                                            @Override
                                            public void done(String s, BmobException e) {
                                                if (e == null){
                                                    Toast.makeText(Mine.this, "添加成功", Toast.LENGTH_SHORT).show();
                                                    //收回添加框
                                                    dialog.changeBankcartLayoutAfterOperate();
                                                    //添加成功后刷新银行卡信息
                                                    BmobQuery<Bankcard> query = new BmobQuery<Bankcard>();
                                                    User user = new User();
                                                    user.setObjectId(keyInfo.getUserID());
                                                    query.addWhereEqualTo("user",new BmobPointer(user));
                                                    query.findObjects(new FindListener<Bankcard>() {
                                                        @Override
                                                        public void done(List<Bankcard> list, BmobException e) {
                                                            if (e == null){
                                                                dialog.setAdapter(list);
                                                            } else{
                                                                Log.d(TAG, "添加银行卡后刷新操作 ："+e.getErrorCode()+e.getMessage());
                                                            }
                                                        }
                                                    });
                                                } else {
                                                    Toast.makeText(Mine.this, "服务器错误", Toast.LENGTH_SHORT).show();
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        });
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
    }

    private void showMineInfoDialog() {
        final MineInfoDialog dialog;
        dialog = new MineInfoDialog(Mine.this,keyInfo.getUserID());
        dialog.show();
    }
}
